import { _decorator, CCFloat, Component, director, instantiate, Label, Node, Prefab, screen, sys } from 'cc'
import { PlayerController } from '../actor/PlayerController'
import { GameEvent } from '../event/gameEvent'
const { ccclass, property } = _decorator
@ccclass('SpawnPoint')
export class SpawnPoint {
    @property(Node)
    spawnPoint: Node = null
    @property(CCFloat)
    intelval: number | null = 5.0
    @property(CCFloat)
    repeatCount: number | null = 0
}

@ccclass('Level')
export class Level extends Component {
    @property([SpawnPoint])
    spawnPoints: SpawnPoint[] = []
    @property(Prefab)
    enemyPrefab: Prefab = null
    totalCount: number = 0
    killedCount: number = 0
    totalEnemyCount: number = 0
    @property(Node)
    uiFail: Node | null = null
    @property(Node)
    uiWin: Node | null = null
    @property(Label)
    statictics: Label | null = null;

    start() {

        if (sys.platform === sys.Platform.MOBILE_BROWSER) {
            screen.requestFullScreen()
        }
        for (let sp of this.spawnPoints) {
            this.totalCount += sp.repeatCount + 1
            this.schedule(() => {
                this.doSpawn(sp)
            }, sp.intelval, sp.repeatCount, 0.0)
        }
        director.on(GameEvent.OnDie, this.onActorDie, this)
        this.statictics.string = `${this.killedCount}/${this.totalCount}`
    }
    doSpawn(sp: SpawnPoint) {
        let node = instantiate(this.enemyPrefab)
        this.node.addChild(node)
        node.worldPosition = sp.spawnPoint.worldPosition
        node.active = true
    }
    onActorDie(node: Node) {
        if (node && node == PlayerController.instance?.node) {
            this.uiFail.active = true
        } else {
            this.killedCount++
            this.statictics.string = `${this.killedCount}/${this.totalCount}`
            // 击杀数量足够 胜利
            if (this.killedCount >= this.totalCount) {
                this.uiWin.active = true
            }
        }
    }
    update(deltaTime: number) {

    }
}

